<?php
declare (strict_types = 1);

namespace app\admin\event;

use Curl\Curl;
use think\facade\Db;
use think\facade\Log;

class UpdateServerEvent
{
    public function updateServer($event)
    {
        $server_data = Db::name('server')->where('company_id', $event['company_id'])->find();
        if(empty($server_data)){
            Log::error('服务器信息不存在!');
            return;
        }
        $company = Db::name('company')->where('id',$event['company_id'])->find();
        $message = '服务器到期';
        $requers_data = [
            'status'=> $company['status'],
            'end_time'=>$company['endtime'],
            'message'=>$message
        ];
        $url = 'http://';
        if(!empty($server_data['host'])){
            $url .= $server_data['host'];
        }else{
            if(!empty($server_data['ip'])){
                $url .= $server_data['ip'];
            }else{
                Log::error('服务器信息不存在!');
                return;
            }
        }
        if(!empty($server_data['port'])){
            $url .= ':'.$server_data['port'];
        }
        $url .= '/server/update';

        $curl = new Curl();
        $curl->post($url, $requers_data);
        if($curl->error){
            Log::error($curl->error_code);
            return;
        }
        $res = $curl->response;
        $curl->close();
        Log::info($res);
    }
}
